﻿Imports AForge.Imaging.Filters
Imports AForge
Imports UCBase
Imports System.Drawing
Imports System.Drawing.Imaging

Public Class ImageServer
    Implements I_ImageExchange
    Implements I_LoadSave
    Private WithEvents SourceEvent As I_ImageExchange
    Private Backpic As New BackgroundCreator
    Private Mov As New Mover
    Private SourceBitmap As System.Drawing.Bitmap
    Private Convert As New UCBase.ColorConverter
    Public Event ImageReady(ByVal Image As System.Drawing.Bitmap) Implements I_ImageExchange.ImageOut1
    Private NetDialog As New NetworkServerSettings
    Delegate Sub TextRec(ByVal cl As Client, ByVal Data As Byte())
    Private RXTxt As TextRec
    Dim Transport As New NetworkTransport
    Private NetworkServer1 As New NetworkServer
    Public Sub New()
        InitializeComponent()
        Me.BackgroundImage = Backpic.CreatePic(Me.Width, Me.Height, Me.BackColor)
        Me.BackColor = Color.Transparent
        Mov.Connect(Me)
        RXTxt = New TextRec(AddressOf NewText)
        AddHandler NetworkServer1.DataReceived, AddressOf NetworkServer_DataReceived
        'NetworkServer1.AutoIP = True
        NetworkServer1.Start()
        Timer1.Enabled = True
    End Sub
    Private Sub SourceEvent_ImageReady(ByVal Image As System.Drawing.Bitmap) Handles SourceEvent.ImageOut1
        SourceBitmap = Convert.ConvertTo24Bit(Image)
        'NetworkServer1.Send("ImageReady")
    End Sub
    Private Sub SocketIn_Connect() Handles SocketIn.Connect
        Dim S As Socket = SocketIn.Remote
        SourceEvent = S.Parent
    End Sub

    Private Sub BlankTransformFilter_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        '233, 116
        Me.Width = 260
        Me.Height = 140
    End Sub
    Public Function GetSettings() As String Implements UCBase.I_LoadSave.GetSettings
        Return ""
    End Function
    Public Sub SetSettings(ByVal Settings As String) Implements UCBase.I_LoadSave.SetSettings

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label6.Text = NetworkServer1.IP
        Label3.Text = NetworkServer1.Port
        If NetworkServer1.Listening = True Then
            Led1.State = Led.LedState.Led_On
        Else
            Led1.State = Led.LedState.Led_Off
        End If
        If NetworkServer1.Connected = True Then
            Led2.State = Led.LedState.Led_On
        Else
            Led2.State = Led.LedState.Led_Off
        End If
    End Sub
    Private Shared Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
        ' Get image codecs for all image formats
        Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
        ' Find the correct image codec
        For i As Integer = 0 To codecs.Length - 1
            If (codecs(i).MimeType = mimeType) Then
                Return codecs(i)
            End If
        Next i
        Return Nothing
    End Function
    Protected Overrides Sub Finalize()
        NetworkServer1.Terminate()
        MyBase.Finalize()
    End Sub

    Private Sub NewText(ByVal CL As Client, ByVal data As Byte())
        If Transport.Decode(data) Then
            If Transport.ContainText Then
                If Transport.Text = "GetImage" Then
                    Led3.State = Led.LedState.Led_On
                    Timer2.Enabled = True
                    NetworkServer1.Send(Transport.Encode(SourceBitmap))
                End If
            End If
        End If

    End Sub

    Private Sub NetworkServer_DataReceived(ByVal Client As Client, ByVal Data() As Byte)
        Invoke(RXTxt, New Object() {Client, Data})
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Timer2.Enabled = False
        Led3.State = Led.LedState.Led_Off
    End Sub

    Private Sub Button3D1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3D1.Click
        NetworkServer1.Terminate()
        If NetworkServer1.AutoIP = True Then
            NetDialog.CheckBox1.Checked = False
        Else
            NetDialog.CheckBox1.Checked = True
        End If
        NetDialog.TextBox1.Text = NetworkServer1.Port
        If NetDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            NetworkServer1.Port = Val(NetDialog.TextBox1.Text)
            If NetDialog.CheckBox1.Checked = True Then
                NetworkServer1.AutoIP = False
            Else
                NetworkServer1.AutoIP = True
            End If
        End If
        NetworkServer1.Start()
    End Sub

End Class
